<template>
  <div class="role-container">
    <div class="left-wrapper">
      <div
        v-for="(item, index) in roleList"
        :key="item.roleId"
        class="role-item"
        :class="{ active: index === activeIndex }"
        @click="changeRole(index)"
      >
        <div class="role-info">
          <svg-icon :icon-class="index === activeIndex ? 'user-active' : 'user'" class="icon" />
          {{ item.roleName }}
        </div>
        <el-dropdown>
          <div class="more">
            <svg-icon icon-class="more" />
          </div>
          <el-dropdown-menu slot="dropdown" size="small">
            <el-dropdown-item @click.native="editRole(item.roleId)">编辑角色</el-dropdown-item>
            <el-dropdown-item style="color: red;" @click.native="delRole(item.roleId)">删除</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>

      </div>
      <el-button size="mini" style="width: 100%;" @click="$router.push('/addRole?type=add')">添加角色</el-button>
    </div>

    <div class="right-wrapper">
      <el-tabs v-model="activeName">
        <el-tab-pane label="功能权限" name="tree">
          <div class="tree-wrapper">
            <div v-for="item in permissionList" :key="item.id" class="tree-item">
              <!-- title -->
              <div class="tree-title">{{ item.title }}</div>
              <!-- 树形结构 -->
              <el-tree
                ref="tree"
                :props="{ label: 'title' }"
                :data="item.children"
                show-checkbox
                :default-expand-all="true"
                node-key="id"
                check-on-click-node
                :expand-on-click-node="false"
              />
            </div>
          </div>
        </el-tab-pane>
        <el-tab-pane :label="title" name="member">
          <div class="user-wrapper">
            <el-table :data="userList" style="width: 100%">
              <el-table-column type="index" width="250" label="序号" />
              <el-table-column prop="name" label="员工姓名" />
              <el-table-column prop="userName" label="登录账号" />
            </el-table>
          </div>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>

<script>
import { getRoleList, deleteRole, getAllPermission, getRoleDetail, getUserListByRoleId } from '@/api/system/role'
import { addDisabled } from '@/utils/index'
export default {
  name: 'Role',
  data() {
    return {
      roleList: [], // 角色列表
      activeIndex: 0, // 当前高亮的角色下标
      permissionList: [], // 权限树形列表
      checkedKeys: [], // 权限列表选中的keys
      activeName: 'tree', // 默认打开tree
      userList: [], // 成员列表
      total: 0 // 成员列表总数
    }
  },
  computed: {
    title() {
      return `成员列表(${this.total})`
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    // 获取角色列表
    async getRoleList() {
      const res = await getRoleList()
      this.roleList = res
      // console.log(res)
      this.getAllPermission()
    },
    // 获取权限列表(树形)
    async getAllPermission() {
      const res = await getAllPermission()
      this.permissionList = addDisabled(res)
      this.getRoleDetail()
      // console.log(res)
    },
    // 切换角色 高亮 依据下标
    async changeRole(idx) {
      this.activeIndex = idx
      // this.checkedKeys = []
      this.getRoleDetail()
    },
    // 获取角色拥有权限
    async getRoleDetail() {
      const roleId = this.roleList[this.activeIndex].roleId
      const res = await getRoleDetail(roleId)
      const { total, rows } = await getUserListByRoleId(roleId)
      this.userList = rows
      this.total = total
      this.checkedKeys = res.perms
      // console.log(res)
      // 调用setCheckedKeys方法
      this.$refs.tree.forEach((tree, index) => {
        tree.setCheckedKeys(this.checkedKeys[index])
      })
    },
    // 删除角色
    async delRole(id) {
      await deleteRole(id)
      this.$message.success('删除成功')
      this.getRoleList()
    },
    // 跳转编辑
    async editRole(id) {
      // console.log(res)
      localStorage.setItem('roleInfo', JSON.stringify(await getRoleDetail(id)))
      this.$router.push(`/addRole?type=edit`)
    }
  }
}
</script>

<style scoped lang="scss">
.role-container {
  display: flex;
  font-size: 14px;
  background-color: #fff;
  padding: 20px;

  .left-wrapper {
    width: 200px;
    border-right: 1px solid #e4e7ec;
    padding: 4px;
    text-align: center;

    .role-item {
      position: relative;
      height: 56px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 20px;
      cursor: pointer;

      &.active {
        background: #edf5ff;
        color: #4770ff;
      }
    }

    .role-info {
      display: flex;
      align-items: center;

      .icon {
        margin-right: 10px;
      }
    }

    .more {
      display: flex;
      align-items: center;
    }

    .addBtn {
      width: 100%;
      margin-top: 20px;
    }
  }

  .right-wrapper {
    flex: 1;

    .tree-wrapper {
      display: flex;
      justify-content: space-between;

      .tree-item {
        flex: 1;
        border-right: 1px solid #e4e7ec;
        padding: 0px 4px;
        text-align: center;

        .tree-title {
          background: #f5f7fa;
          text-align: center;
          padding: 10px 0;
          margin-bottom: 12px;
        }
      }
    }

    ::v-deep .el-tabs__nav-wrap {
      padding-left: 20px;
    }

    .user-wrapper {
      padding: 20px;
    }
  }
}
</style>
